package com.tarena.lbs.basic.web.dao.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.dao.mapper.UserGroupMapper;
import com.tarena.lbs.basic.web.dao.repository.UserGroupRepository;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.dos.UserGroupDO;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class UserGroupRepositoryImpl implements UserGroupRepository {
    @Autowired
    private UserGroupMapper userGroupMapper;

    @Override
    public BasePageList<UserGroupBO> getUserGroupByPage(UserGroupQuery userGroupQuery) {
        //分页查询
        PageHelper.startPage(userGroupQuery.getPageNo(),userGroupQuery.getPageSize());
        List<UserGroupBO> userGroupBOS= userGroupMapper.selectAll();
        PageInfo<UserGroupBO> pageInfo = new PageInfo<>(userGroupBOS);
        BasePageList<UserGroupBO> userGroupPage=null;
        if (pageInfo.getTotal()>0){
            List<UserGroupBO> list = pageInfo.getList();
            List<UserGroupBO> bos = list.stream().map(aDo -> {
                UserGroupBO userGroupBO = new UserGroupBO();
                BeanUtils.copyProperties(aDo, userGroupBO);
                return userGroupBO;
            }).collect(Collectors.toList());
            userGroupPage=new BasePageList<>(bos, pageInfo.getTotal());
        }else {
            //没找到
            userGroupPage = new BasePageList<>(null,0L);
        }
        return userGroupPage;
    }

    @Override
    public void insertUserGroup(UserGroupDO userGroupDO) {
        userGroupMapper.insertUserGroup(userGroupDO);
    }
}
